package com.jkc.xh.fragments;

import android.graphics.Matrix;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.AssetInfoResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.headview.HeadMyMoney;
import com.jkc.xh.views.widget.NavBar;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_my_factory)
public class MyFactoryFragment extends BaseFragment {
    @ViewById(R.id.pager)
    ViewPager pager;
    @ViewById(R.id.radioGroup_tag_name)
    RadioGroup rgTagName;
    @ViewById(R.id.iv_cursor)
    ImageView iv_cursor;
    @ViewById(R.id.nav_bar)
    NavBar mNavBar;
    @ViewById(R.id.head_money)
    HeadMyMoney headMoney;
    private RadioButton radioButton;


    int moveX; // 导航下面横线偏移宽度
    int width; // 导航下面比较粗的线的宽度
    int index; // 当前第一个view
    private String[] titles = new String[]{"分笔持仓", "合并持仓"};
    private ArrayList<Fragment> views = new ArrayList<Fragment>();
    private MyFactoryChildFragment holdOneFragment;
    private MyFactoryHoldAllFragment holdAllFragment;

    @AfterViews
    public void initData() {
        mNavBar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                refreshData();
                if (holdOneFragment != null) {
                    holdOneFragment.requestData(true);
                }
                if (holdAllFragment != null) {
                    holdAllFragment.requestData(true);
                }
            }
        });
        holdOneFragment = MyFactoryChildFragment_.builder().build();
        holdAllFragment = MyFactoryHoldAllFragment_.builder().build();
        views.add(holdOneFragment);
        views.add(holdAllFragment);


        RadioGroup.LayoutParams rbtnParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1);

        for (int i = 0; i < titles.length; i++) {
            radioButton = new RadioButton(getActivity());
            radioButton.setId(i);
            radioButton.setButtonDrawable(android.R.color.transparent);
            radioButton.setText(titles[i]);
            radioButton.setClickable(true);
//            if (i == 0) {
//                radioButton.setTextColor(getResources().getColor(R.color.litter_black));
//            } else {
//                radioButton.setTextColor(Color.GRAY);
//            }
            radioButton.setGravity(Gravity.CENTER);
            rgTagName.addView(radioButton, rbtnParams);
        }

        rgTagName.setOnCheckedChangeListener(tabChangeListener);
        /* 初始化view的引导横线 */
        DisplayMetrics dm = new DisplayMetrics(); // 获取手机分辨率
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度

        ViewGroup.LayoutParams para = iv_cursor.getLayoutParams();
        para.width = screenW / titles.length;
        iv_cursor.setLayoutParams(para);
        // 计算偏移量，也就是那条粗的下划线距离屏幕坐标的距离，如果有三个view则screeW/3，以此类推
        moveX = (screenW / 2 - width) / 2;
        Matrix matrix = new Matrix();
        matrix.postTranslate(moveX, 0);
        iv_cursor.setImageMatrix(matrix); // 设置动画初始位置
        iv_cursor.setVisibility(View.VISIBLE);

        FragmentPagerAdapter adapter = new TabFragmentAdapter();
        pager.setAdapter(adapter);
        pager.setOnPageChangeListener(new MyPageListener());
        refreshData();
    }

    private void refreshData() {
        addJob(TraderManager.queryAssetInfo(new RestCallBack<AssetInfoResponse>() {

            @Override
            public void success(AssetInfoResponse assetInfoResponse) {
                headMoney.loadInfo(assetInfoResponse);
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }

    RadioGroup.OnCheckedChangeListener tabChangeListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int j = 0; j < group.getChildCount(); j++) {
                radioButton = (RadioButton) group.getChildAt(j);
                if (j == checkedId) {
                    pager.setCurrentItem(j);
                }
            }
        }
    };

    class MyPageListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int toIndex) {
            // recommendLayout.setResettingScrollView();
            int x = moveX * 2 + width; // 从第一个到第二个view，粗的下划线的偏移量

            Animation animation = new TranslateAnimation(x * index, x * toIndex, 0, 0);
            index = toIndex;
            animation.setFillAfter(true); // 设置动画停止在结束位置
            animation.setDuration(300); // 设置动画时间
            iv_cursor.startAnimation(animation); // 启动动画
            for (int current = 0; current < rgTagName.getChildCount(); current++) {
                radioButton = (RadioButton) rgTagName.getChildAt(current);
//                if (current == toIndex) {
//                    radioButton.setTextColor(getResources().getColor(R.color.litter_black));
//                } else {
//                    radioButton.setTextColor(Color.GRAY);
//                }
            }
        }
    }

    private class TabFragmentAdapter extends FragmentPagerAdapter {

        public TabFragmentAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return views.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }

}
